package l1j.server.server.serverpackets;

import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.Config; //TODO GM使用公頻(&)顯示方式設定 4/5
//Referenced classes of package l1j.server.server.serverpackets:
//ServerBasePacket

public class S_ChatPacket extends ServerBasePacket {

	private static final String _S__1F_NORMALCHATPACK = "[S] S_ChatPacket";
	private byte[] _byte = null;

	public S_ChatPacket(String name, String chat, int opcode){
		writeC(opcode);
		writeC(0x09);
		writeS("-> (" + name + ") " + chat);
	}
	public S_ChatPacket(final L1PcInstance pc, final String chat,final int opcode, final int type) {
		final StringBuilder msg = new StringBuilder();

		switch (type) {
		case 0:
			// 通常チャット
			this.writeC(opcode);
			this.writeC(type);
			if (pc.isInvisble()) {
				this.writeD(0);
			} else {
				this.writeD(pc.getId());
			}
			this.writeS(msg.append(pc.getName()).append(": ").append(chat).toString());
			break;
		case 2: // 叫び
			this.writeC(opcode);
			this.writeC(type);
			if (pc.isInvisble()) {
				this.writeD(0);
			} else {
				this.writeD(pc.getId());
			}
			this.writeS(msg.append("<").append(pc.getName()).append("> ").append(chat).toString());
			this.writeH(pc.getX());
			this.writeH(pc.getY());
			// TODO 說話時加上頭銜 byttt999
			break;
		case 3:
			// 全體
			this.writeC(opcode);
			this.writeC(type);
			if (pc.isGm() || pc.isMonitor()) { // GM
				// TODO change GM使用公頻(&)顯示方式設定 5/5
				if (Config.GMTalkShowName) {
					this.writeS("\\fY"+ msg.append("[").append(pc.getName()).append("] ").append(chat).toString());
					// writeS("[*" + pc.getName() + "*] " + chat);
				} else {
					this.writeS("\\fY"
							+ msg.append("[******] ").append(chat)
							.toString());
					// writeS("[******] " + chat);
				}
				// writeS(msg.append("\\fY[******] \\fW").append(chat).toString());
				// writeS("\\fY" + "[******] " + "\\fW" + chat);
			} else { // TODO 不是GM,沒有血盟
				this.writeS(msg.append("[").append(pc.getName())
						.append("] ").append(chat).toString());
				// writeS("[" + pc.getName() + "] " + chat);
			}
			break;
		case 4:
			// 血盟チャット
			this.writeC(opcode);
			this.writeC(type);
			// 2:一般 3:副君主 4:聯盟君主 5:修習騎士 6:守護騎士 7:一般 8:修習騎士 9:守護騎士 10:聯盟君主
			final int rank = pc.getClanRank();
			if(rank>2 && rank!=5 && rank!=7 && rank!=8){
			this.writeS(msg.append(pc.getClan().getRankString(pc)).append("{").append(pc.getName()).append("} ")
					.append(chat).toString());
			}else{
				this.writeS(msg.append("{").append(pc.getName()).append("} ").append(chat).toString());
			}
			// writeS("{" + pc.getName() + "} " + chat);
			break;
		case 9:
			// ウィスパー
			this.writeC(opcode);
			this.writeC(type);
			this.writeS(msg.append("-> (").append(pc.getName())
					.append(") ").append(chat).toString());
			// writeS("-> (" + pc.getName() + ") " + chat);
			break;
		case 11:
			// パーティーチャット
			this.writeC(opcode);
			this.writeC(type);
			this.writeS(msg.append("(").append(pc.getName()).append(") ")
					.append(chat).toString());
			// writeS("(" + pc.getName() + ") " + chat);
			break;
		case 12: // トレードチャット
			this.writeC(opcode);
			this.writeC(type);
			this.writeS(msg.append("[").append(pc.getName()).append("] ")
					.append(chat).toString());
			// writeS("[" + pc.getName() + "] " + chat);
			break;
		case 13:// 連合チャット
			this.writeC(opcode);
			this.writeC(type);
			this.writeS(msg.append("{{").append(pc.getName()).append("}} ").append(chat).toString());
			// writeS("{{" + pc.getName() + "}} " + chat);
			break;
		case 14:// チャットパーティー
			this.writeC(opcode);
			this.writeC(type);
			if (pc.isInvisble()) {
				this.writeD(0);
			} else {
				this.writeD(pc.getId());
			}
			this.writeS(msg.append("(").append(pc.getName()).append(") ").append(chat).toString());
			// writeS("(" + pc.getName() + ") " + chat);
			break;
		case 16:// ウィスパー
			this.writeC(opcode);
			this.writeS(pc.getName());
			this.writeS(chat);
			break;
		default:
			break;
		}
		msg.delete(0, msg.length());
		/*
		 * if (type == 0) { // 通常チャット writeC(opcode); writeC(type); if
		 * (pc.isInvisble()) { writeD(0); } else { writeD(pc.getId()); }
		 * writeS(pc.getName() + ": " + chat); } else if (type == 2) { // 叫び
		 * writeC(opcode); writeC(type); if (pc.isInvisble()) { writeD(0); }
		 * else { writeD(pc.getId()); } writeS("<" + pc.getName() + "> " +
		 * chat); writeH(pc.getX()); writeH(pc.getY()); // TODO 說話時加上頭銜 byttt999
		 * } else if (type == 3){ // 全體
		 * 
		 * final String clan_name = pc.getClanname(); final L1Clan clan =
		 * L1World.getInstance().getClan(clan_name); writeC(opcode);
		 * writeC(type); if (pc.isGm()||pc.isMonitor()) { // GM // TODO change
		 * GM使用公頻(&)顯示方式設定 5/5 if (Config.GMTalkShowName) { writeS("[*" +
		 * pc.getName() + "*] " + chat); } else { writeS("[******] " + chat); }
		 * writeS("\\fY" + "[******] " + "\\fW" + chat); } else if
		 * (pc.getClanid() != 0) { // 有血盟 if (pc.getId() == clan.getLeaderId()
		 * && clan.getCastleId() == 1) { // TODO 肯特城 writeS("\\fR" + "肯特城主[" +
		 * pc.getName() + "] " + chat); } else if (pc.getId() ==
		 * clan.getLeaderId() && clan.getCastleId() == 2) { // TODO 妖堡
		 * writeS("\\fB" + "妖魔城主[" + pc.getName() + "] " + chat); } else if
		 * (pc.getId() == clan.getLeaderId() && clan.getCastleId() == 3) { //
		 * TODO 風木 writeS("\\fU" + "風木城主[" + pc.getName() + "] " + chat); } else
		 * if (pc.getId() == clan.getLeaderId() && clan.getCastleId() == 4) { //
		 * TODO 奇岩 writeS("\\fT" + "奇岩城主[" + pc.getName() + "] " + chat); } else
		 * if (pc.getId() == clan.getLeaderId() && clan.getCastleId() == 5) { //
		 * TODO 海音 writeS("\\fW" + "海音城主[" + pc.getName() + "] " + chat); } else
		 * if (pc.getId() == clan.getLeaderId() && clan.getCastleId() == 6) { //
		 * TODO 侏儒 writeS("\\fJ" + "侏儒城主[" + pc.getName() + "] " + chat); } else
		 * if (pc.getId() == clan.getLeaderId() && clan.getCastleId() == 7) { //
		 * TODO 亞丁 writeS("\\fG" + "亞丁城主[" + pc.getName() + "] " + chat); } else
		 * if (pc.getId() == clan.getLeaderId() && clan.getCastleId() == 8) { //
		 * TODO 狄亞得要塞 writeS("\\fO" + "狄亞得要塞城主[" + pc.getName() + "] " + chat);
		 * } else if (pc.getId() == clan.getLeaderId() && clan.getCastleId() ==
		 * 0) { // TODO 沒城堡,有血盟 writeS("\\fS" + pc.getClanname() + "盟主[" +
		 * pc.getName() + "] " + chat); } else { // TODO 血盟成員
		 * writeS(pc.getClanname() + "盟[" + pc.getName() + "] " + chat); } }
		 * else { // TODO 不是GM,沒有血盟 writeS("[" + pc.getName() + "] " + chat); }
		 * } else if (type == 4) { // 血盟チャット writeC(opcode); writeC(type);
		 * writeS("{" + pc.getName() + "} " + chat); } else if (type == 9) { //
		 * ウィスパー writeC(opcode); writeC(type); writeS("-> (" + pc.getName() +
		 * ") " + chat); } else if (type == 11) { // パーティーチャット writeC(opcode);
		 * writeC(type); writeS("(" + pc.getName() + ") " + chat); } else if
		 * (type == 12) { // トレードチャット writeC(opcode); writeC(type); writeS("[" +
		 * pc.getName() + "] " + chat); } else if (type == 13) { // 連合チャット
		 * writeC(opcode); writeC(type); writeS("{{" + pc.getName() + "}} " +
		 * chat); } else if (type == 14) { // チャットパーティー writeC(opcode);
		 * writeC(type); if (pc.isInvisble()) { writeD(0); } else {
		 * writeD(pc.getId()); } writeS("(" + pc.getName() + ") " + chat); }
		 * else if (type == 16) { // ウィスパー writeC(opcode); writeS(pc.getName());
		 * writeS(chat); }
		 */
	}

	@Override
	public byte[] getContent() {
		if (null == this._byte) {
			this._byte = this._bao.toByteArray();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return _S__1F_NORMALCHATPACK;
	}

}